package com.lul15834.orderfood;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.lul15834.orderfood.activity.manager.Comment;
import com.lul15834.orderfood.activity.manager.Manager;
import com.lul15834.orderfood.activity.manager.OrderFinish;
import com.lul15834.orderfood.activity.manager.OrderNoFinish;
import com.lul15834.orderfood.activity.manager.RegisterManager;
import com.lul15834.orderfood.activity.user.ManageUser;
import com.lul15834.orderfood.activity.user.RegisterUser;
import com.lul15834.orderfood.activity.user.UserAddress;
import com.lul15834.orderfood.activity.user.UserComment;
import com.lul15834.orderfood.dao.AdminDao;
import com.lul15834.orderfood.db.DBUntil;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DBUntil dbUntil=new DBUntil(this);
        DBUntil.con=dbUntil.getWritableDatabase();
        //实现共享数据
        SharedPreferences sharedPreferences=getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit=sharedPreferences.edit();

        RadioButton sjRadio=findViewById(R.id.login_merchant);
        sjRadio.setChecked(true);//默认选择商家
        Button regMerchant=findViewById(R.id.login_register);
        regMerchant.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到注册商家页面
                Intent intent=new Intent(MainActivity.this, RegisterManager.class);
                startActivity(intent);
            }
        });
        Button regUser=findViewById(R.id.login_registerUser);
        regUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到注册用户页面
                Intent intent=new Intent(MainActivity.this, RegisterUser.class);
                startActivity(intent);
            }
        });
        //登录功能
        EditText accountText=findViewById(R.id.login_account);
        EditText pwdText=findViewById(R.id.login_pwd);
        Button login=findViewById(R.id.login_login);
        RadioButton merchant=findViewById(R.id.login_merchant);
        RadioButton user=findViewById(R.id.login_user);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account=accountText.getText().toString();
                String pwd=pwdText.getText().toString();
                if (account.isEmpty()){
                    Toast.makeText(MainActivity.this,"请输入账号",Toast.LENGTH_SHORT).show();
                }else if (pwd.isEmpty()){
                    Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                }else {
                    edit.putString("account",account);//存储账号键值对
                    edit.apply();
                    if (merchant.isChecked()){
                        //管理员
                        int a= AdminDao.loginBusiness(account,pwd);
                        if (a==1){
                            Toast.makeText(MainActivity.this,"商家登录成功",Toast.LENGTH_SHORT).show();
                                    Intent intent=new Intent(MainActivity.this, Manager.class);
                                    startActivity(intent);
                        }else {
                            Toast.makeText(MainActivity.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
                        }

                    }
                    if (user.isChecked()){
                        int a=AdminDao.loginUser(account,pwd);
                        if (a==1){
                            Toast.makeText(MainActivity.this,"用户登录成功",Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(MainActivity.this, ManageUser.class);
                            startActivity(intent);
                        }else {
                            Toast.makeText(MainActivity.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
                        }

                    }
                }
            }
        });
    }
}